//
//  MathTools.mm
//  FrameWork
//
//  Created by Eagle on 6/30/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

float GetDegree(float x, float y)
{
	float dist = 0;
	dist  = sqrtf(x*x + y*y);
	float ccos = x/dist;
	
	float degree = acosf(ccos)*180/M_PI;
	
	if (y < 0) 
	{
		degree = 360 - degree;
	}
	
	return degree;
	
}


int GetAreaFromDegree(float degree)
{
	if (fabs(degree - 90.f) <= 11.25) 
	{
		return 0;
	}
	else if (fabs(degree - 112.5) <= 11.25) 
	{
		return 1;
	}
	else if (fabs(degree - 135) <= 11.25) 
	{
		return 2;
	}

	else if (fabs(degree - 157.5) <= 11.25) 
	{
		return 3;
	}

	else if (fabs(degree - 180.f) <= 11.25) 
	{
		return 4;
	}

	else if (fabs(degree - 202.5) <= 11.25) 
	{
		return 5;
	}

	else if (fabs(degree - 225.f) <= 11.25) 
	{
		return 6;
	}

	else if (fabs(degree - 247.5) <= 11.25) 
	{
		return 7;
	}
	
	else if (fabs(degree - 270) <= 11.25) 
	{
		return 8;
	}
	
	else if (fabs(degree - 292.5) <= 11.25) 
	{
		return 7;
	}
	
	else if (fabs(degree - 315.f) <= 11.25) 
	{
		return 6;
	}
	
	else if (fabs(degree - 337.5) <= 11.25) 
	{
		return 5;
	}
	
	else if (fabs(degree - 360) <= 11.25) 
	{
		return 4;
	}
	
	else if (fabs(degree - 0) <= 11.25) 
	{
		return 4;
	}
	
	else if (fabs(degree - 22.5) <= 11.25) 
	{
		return 3;
	}
	
	else if (fabs(degree - 45) <= 11.25) 
	{
		return 2;
	}
	
	else if (fabs(degree - 67.5) <= 11.25) 
	{
		return 1;
	}
	return 0;
	
}

//判断是否需要镜像
bool IsNeedFlipX(float degree)
{
	if (degree > 270 || degree <90) 
	{
		return true;
	}
	
	return false;
}

//判断是顺时针旋转还是逆时针旋转 0为不旋转,1 顺时针, 2 逆时针
int ClockOrWiseClock(float degree1, float degree2)
{
	if (degree2 > degree1)
	{
		if ((degree2 - degree1) > 180) 
		{
			return 1;
		}
		else 
		{
			return 2;
		}
	}
	else if(degree2 < degree1)
	{
		if ((degree1 - degree2) > 180) 
		{
			return 2;
		}
		else 
		{
			return 1;
		}

	}
	return 0;
}



//计算绝对角度差, 1 - 180
float GetAbsDegree(float degree1, float degree2)
{
	if (degree1 == degree2) 
	{
		return 0;
	}
	else if(degree1 > degree2)
	{
		float delta = degree1 - degree2;
		if (delta > 180) 
		{
			return 360 - delta;
		}
		else 
		{
			return delta;
		}

	}
	else if(degree1 < degree2)
	{
		float delta = degree2 - degree1;
		if (delta > 180) 
		{
			return 360 - delta;
		}
		else 
		{
			return delta;
		}
	}
	return 0;
}







